package com.framework.config;

import java.io.IOException;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

/**
 * 루트 설정용 클래스. 이 클래스는 스프링의 root-context.xml 의 역할을 대신한다.
 */
@Configuration
public class RootConfig {

	private static final String CONTENT_CONFIG_FILE_PATH = "content-config.xml";

	private static final String FILE_CONFIG_FILE_PATH = "file-config.xml";

	@Value("${tempFolder}")
	private String tempFolder;

	/**
	 * 프로퍼티 홀더는 다른 빈들이 사용하는 프로퍼티들을 로딩하기 때문에, static 메소드로 실행된다.
	 * 다른 일반 빈들이 만들어지기전에 먼저 만들어져야 한다.
	 *
	 * @return
	 */
	@Bean
	public static PropertyPlaceholderConfigurer propertyPlaceholderConfigurer() {

		PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
		ppc.setLocations(new Resource[] {
				new ClassPathResource(CONTENT_CONFIG_FILE_PATH)
				, new ClassPathResource(FILE_CONFIG_FILE_PATH)
				});
		return ppc;
	}

	@Bean
	public CommonsMultipartResolver commonsMultipartResolver() throws IOException{
		CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
		multipartResolver.setDefaultEncoding("UTF-8");
		multipartResolver.setMaxUploadSize(102400000);
		multipartResolver.setUploadTempDir(new FileSystemResource(tempFolder));
		return multipartResolver;
	}
}
